package com.example.demo.excelprint;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import lombok.extern.slf4j.Slf4j;


/**
 * @author YunGang.Guo
 * @date 2022/01/05 16:44
 **/
@Slf4j
public class ExcelToPDFUtil {

    /**
     * 使用jacob实现excel转PDF
     *
     * @param inputFilePath  导入Excel文件路径
     * @param outputFilePath 导出PDF文件路径
     */
    public static void jacobExcelToPDF(String inputFilePath, String outputFilePath,PrintConfig printConfig) {
        ActiveXComponent ax = null;
        Dispatch excel = null;

        try {
            ComThread.InitSTA();
            ax = new ActiveXComponent("Excel.Application");
            ax.setProperty("Visible", new Variant(false));
            //禁用宏
            ax.setProperty("AutomationSecurity", new Variant(3));

            Dispatch excels = ax.getProperty("Workbooks").toDispatch();

            Object[] obj = {
                    inputFilePath,
                    new Variant(false),
                    new Variant(false)
            };

            excel = Dispatch.invoke(excels, "Open", Dispatch.Method, obj, new int[9]).toDispatch();

            //获取到sheets的集合对象
            Dispatch sheets = Dispatch.get(excel, "sheets").toDispatch();
            //获取到总表数
            int count = Dispatch.get(sheets, "count").changeType(Variant.VariantInt).getInt();
            //只打印第1个sheet页
            int i=1;
            //获取到sheet页
            Dispatch workSheet = Dispatch.invoke(sheets, "Item", Dispatch.Get, new Object[]{i}, new int[1]).toDispatch();

            Dispatch HPageBreaks = Dispatch.get(workSheet, "HPageBreaks").toDispatch();
            int HPageBreaksCount = Dispatch.get(HPageBreaks, "count").changeType(Variant.VariantInt).getInt();
            System.out.println("共有"+HPageBreaksCount+"条分页符");

            Dispatch page = Dispatch.get(workSheet, "PageSetup").toDispatch();

            //是否设置区域打印
//            Dispatch.put(page, "PrintArea", "$A$1:$B$S");
            Dispatch.put(page, "PrintArea", printConfig.getPrintArea());
            //设置横向打印还是纵向打印 2横向 1纵向
            Dispatch.put(page, "Orientation", printConfig.getOrientation());
            //设置缩放，值为100或false
            Dispatch.put(page, "Zoom", printConfig.getZoom());
            //所有行为一页
            Dispatch.put(page, "FitToPagesTall", false);
            //所有列为一页(1或false)
            Dispatch.put(page, "FitToPagesWide", false);
            Dispatch.put(page, "Order", printConfig.getOrder());
            /**
             *
             */
            Dispatch.put(page, "PrintTitleColumns",printConfig.getPrintTitleColumns() );
//            Variant pageBreaks = Dispatch.get(page, "HPageBreak");
//            System.out.println(pageBreaks);
//            Dispatch.put(page, "PrintTitleColumns", "A:E");
            log.info("sheet页[" + i + "]设置成功，");


            //转换格式
            Object[] obj2 = {
                    //PDF格式等于0
                    new Variant(0),
                    outputFilePath,
                    //0=标准（生成的PDF图片不会模糊），1=最小的文件
                    new Variant(0)
            };
            Dispatch.invoke(excel, "ExportAsFixedFormat", Dispatch.Method, obj2, new int[1]);

        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            if (excel != null) {
                Dispatch.call(excel, "Close", new Variant(false));
            }
            if (ax != null) {
                ax.invoke("Quit", new Variant[]{});
                ax = null;
            }
            ComThread.Release();
        }
    }
}

